package com.hanxiaozhang;

import com.hanxiaozhang.exception.DistributedLockException;

import java.util.concurrent.TimeUnit;

/**
 * 功能描述: <br>
 * 〈分布式锁接口〉
 *
 * @Author:hanxinghua
 * @Date: 2022/8/29
 */
public interface DistributedLock {

    /**
     * 获取锁key
     *
     * @return
     */
    String getLockKey();

    /**
     * 加锁
     *
     * @throws DistributedLockException
     */
    void lock() throws DistributedLockException;


    /**
     * 可以判断中断的加锁
     * 除非当前线程是中断，否则获取锁
     *
     * @throws DistributedLockException
     * @throws InterruptedException
     */
    void lockInterruptibly() throws DistributedLockException, InterruptedException;


    /**
     * 尝试加锁
     *
     * @return
     * @throws DistributedLockException
     */
    Boolean tryLock() throws DistributedLockException;


    /**
     * 尝试加锁
     *
     * @param time
     * @param timeUnit
     * @return
     * @throws DistributedLockException
     * @throws InterruptedException
     */
    Boolean tryLock(long time, TimeUnit timeUnit) throws DistributedLockException, InterruptedException;


    /**
     * 解锁
     *
     * @return
     * @throws DistributedLockException
     */
    void unlock() throws DistributedLockException;

}
